package com.handong.handserialprogram.module.setting;

import android.support.annotation.NonNull;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.handong.handserialprogram.R;
import com.handong.handserialprogram.module.base.BackStackFragment;
import com.handong.handserialprogram.utils.AppUtil;
import com.handong.handserialprogram.utils.SPUtil;

/**
 * ModifySysPsdFragment.
 */
public class ModifySysPsdFragment extends BackStackFragment {

    private final int INPUT_NUMBER_LIMIT = 6;
    private EditText mOldPsd, mNewPsd, mSureNewPsd;

    public static ModifySysPsdFragment newInstance() {
        ModifySysPsdFragment fragment = new ModifySysPsdFragment();
        return fragment;
    }

    @Override
    protected int getContentViewID() {
        return R.layout.fragment_modify_sys_psd;
    }

    @Override
    protected void initViewByID(View contentView) {
        mOldPsd = (EditText) contentView.findViewById(R.id.modifyOldPsd);
        mNewPsd = (EditText) contentView.findViewById(R.id.modifyNewPsd);
        mSureNewPsd = (EditText) contentView.findViewById(R.id.modifySureNewPsd);
        mOldPsd.setFilters(new InputFilter[]{new InputFilter.LengthFilter(INPUT_NUMBER_LIMIT)});
        mNewPsd.setFilters(new InputFilter[]{new InputFilter.LengthFilter(INPUT_NUMBER_LIMIT)});
        mSureNewPsd.setFilters(new InputFilter[]{new InputFilter.LengthFilter(INPUT_NUMBER_LIMIT)});
        mOldPsd.requestFocus();
        AppUtil.showSoftInput(mOldPsd);

        contentView.findViewById(R.id.headerOptionLayout).setOnClickListener(this);
        contentView.findViewById(R.id.headerBackLayout).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.headerOptionLayout:
                dealInputPsd();
                break;
            default:
                super.onClick(v);
        }
    }

    private void dealInputPsd() {
        String oldPsd = mOldPsd.getText().toString().trim();
        String newPsd = mNewPsd.getText().toString().trim();
        String sureNewPsd = mSureNewPsd.getText().toString().trim();
        if (TextUtils.isEmpty(oldPsd)) {
            AppUtil.showShortToast(getActivity(), "原密码不能为空");
            return;
        }
        if (TextUtils.isEmpty(newPsd)) {
            AppUtil.showShortToast(getActivity(), "新密码不能为空");
            return;
        }
        if (TextUtils.isEmpty(sureNewPsd)) {
            AppUtil.showShortToast(getActivity(), "确认密码不能为空");
            return;
        }
        if (!oldPsd.equals(SPUtil.getStringValue(SPUtil.USER_PASSWORD, "123456"))) {
            AppUtil.showShortToast(getActivity(), "原密码不正确");
            return;
        }
        if (!newPsd.equals(sureNewPsd)) {
            AppUtil.showShortToast(getActivity(), "两次输入新密码不相同");
            return;
        }
        SPUtil.putStringValue(SPUtil.USER_PASSWORD, newPsd);
        removeFrag(this);
        AppUtil.showShortToast(getActivity(), "修改密码成功");
    }

    @Override
    protected boolean onBackClicked() {
        String oldPsd = mOldPsd.getText().toString().trim();
        String newPsd = mNewPsd.getText().toString().trim();
        String sureNewPsd = mSureNewPsd.getText().toString().trim();
        if (!TextUtils.isEmpty(oldPsd) || !TextUtils.isEmpty(newPsd) || !TextUtils.isEmpty(sureNewPsd)) {
            MaterialDialog dialog = new MaterialDialog.Builder(getActivity())
                    .content("是否放弃已输入内容，直接退出？")
                    .positiveText(R.string.dialog_positive_text)
                    .positiveColorRes(R.color.back_ground_green)
                    .negativeText(R.string.dialog_negative_text)
                    .negativeColorRes(R.color.text_content)
                    .onPositive(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                            removeFrag(ModifySysPsdFragment.this);
                        }
                    }).build();
            dialog.show();
            return true;
        }
        return super.onBackClicked();
    }

    @Override
    public void onDestroyView() {
        AppUtil.hideSoftInput(getActivity(), getActivity().getWindow());
        super.onDestroyView();
    }
}
